package com.example.chapter09;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.example.chapter09.receiver.NetworkConnectChangedReceiver;
import com.example.chapter09.service.IGetMessageCallBack;
import com.example.chapter09.service.MQTTService;
import com.example.chapter09.service.MyServiceConnection;


public class MqttActivity extends AppCompatActivity implements IGetMessageCallBack, View.OnClickListener {

    private TextView textView;
    private Button button;
    private MyServiceConnection serviceConnection;
    private MQTTService mqttService;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_mqtt);
        textView = (TextView) findViewById(R.id.bobText);
        findViewById(R.id.bobButton).setOnClickListener(this);
        serviceConnection = new MyServiceConnection();
        serviceConnection.setIGetMessageCallBack(MqttActivity.this);
        Intent intent = new Intent(this, MQTTService.class);
        bindService(intent, serviceConnection, Context.BIND_AUTO_CREATE);
    }

    @Override
    public void setMessage(String message) {
        textView.setText(message);
        mqttService = serviceConnection.getMqttService();
        mqttService.toCreateNotification(message);
    }

    @Override
    protected void onDestroy() {
        unbindService(serviceConnection);
        super.onDestroy();
    }

    @Override
    public void onClick(View view) {
        MQTTService.publish("测试一下子");
    }
}